package ibmetp.mis.dao;

import ibmetp.mis.common.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 
 * @author IBMETP061 LIN A ER
 * @created 2011-10-13
 *
 */
public class ResToRoleDao {
	private Connection conn;
    /**
     * 通过roleIds和resourceIds为角色配置资源
     * @param roleIds
     * @param resourceIds
     * @return
     */
	public void addResRole(String roleIds,String resourceIds)
	{
		String[] roleids=JdbcUtil.string2Array(roleIds);
		String[] resourceids=JdbcUtil.string2Array(resourceIds);
		for(int i=0;i<roleids.length;i++)
		{
			for(int j=0;j<resourceids.length;j++)
			{
				String sql="insert into BAS_T_ROLE_RESOURCE(RESOURCE_ROLE_ID,ROLE_ID,RESOURCE_ID,CREATE_DATE) values(ROLE_RES_SEQ.NEXTVAL,?,?,sysdate)";
				conn=DBUtil.getConnection();
				PreparedStatement ps;
				
				try {
					ps=conn.prepareStatement(sql);
					ps.setLong(1, Long.valueOf(roleids[i]));
					ps.setLong(2, Long.valueOf(resourceids[j]));
				    ResultSet rs=ps.executeQuery();
				    ps.close();
				    conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 通过roleIds和resourceIds为角色卸载资源
	 * @param roleIds
	 * @param resourceIds
	 * @return
	 */
	public void removeResRole(String roleIds,String resourceIds)
	{
		String[] roleids=JdbcUtil.string2Array(roleIds);
		String[] resourceids=JdbcUtil.string2Array(resourceIds);
		for(int i=0;i<roleids.length;i++)
		{
			for(int j=0;j<resourceids.length;j++)
			{
				String sql="delete from BAS_T_ROLE_RESOURCE where RESOURCE_ID=? and ROLE_ID=?";
				conn=DBUtil.getConnection();
				PreparedStatement ps;
				
				try {
					ps=conn.prepareStatement(sql);
					ps.setLong(1, Long.valueOf(resourceids[j]));
					ps.setLong(2, Long.valueOf(roleids[i]));
				    ResultSet rs=ps.executeQuery();
				    ps.close();
				    conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
